package com.cloudspace.jindun.activity.xiaoxi;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.model.CommonResult;
import com.cloudspace.jindun.model.GroupsDetail;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class HuatiZiliaoActivity extends CustomTitleBaseActivity {

    @Bind(R.id.huati_title_back)
    ImageView mHuatiTitleBack;
    @Bind(R.id.rl_text_title)
    TextView mRlTextTitle;
    @Bind(R.id.phone_tv_huati_name)
    TextView mPhoneTvHuatiName;
    @Bind(R.id.phone_tv_huati_id)
    TextView mPhoneTvHuatiId;
    @Bind(R.id.phone_tv_huati_member)
    TextView mPhoneTvHuatiMember;
    @Bind(R.id.phone_btn_huati_delete)
    Button mPhoneBtnHuatiDelete;

    String qunID;
    String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xiaoxi_huati_details, false);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        qunID=getIntent().getStringExtra("qunID");
        type=getIntent().getStringExtra("type");
        if (!TextUtils.isEmpty(qunID)){
            if (type != null) {
                switch (type){
                    case "1":
                        mRlTextTitle.setText(getResources().getString(R.string.top_info));
                        mPhoneBtnHuatiDelete.setText(getResources().getString(R.string.huati_delete));
                        requestData(YXHttpUrl.XX_HQHUATIZILIAO,qunID);
                        break;
                    case "2":
                        mRlTextTitle.setText(getResources().getString(R.string.group_chat_info));
                        mPhoneBtnHuatiDelete.setText(getResources().getString(R.string.groupchat_delete));
                        requestData(YXHttpUrl.XX_HQQZL,qunID);
                        break;

                    default:
                        break;
                }
            }
        }
    }

    @OnClick({R.id.huati_title_back, R.id.phone_btn_huati_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.huati_title_back:
                finish();
                break;
            case R.id.phone_btn_huati_delete:
                if (!TextUtils.isEmpty(qunID)){

                    switch (type){
                        case "1":
                            deleQUN(YXHttpUrl.XX_TCHUATI, UserSessionManager.getInstance().getUserId(),qunID);
                            break;
                        case "2":
                            deleQUN(YXHttpUrl.XX_TCQUNLIAO,UserSessionManager.getInstance().getUserId(),qunID);
                            break;
                    }

                }
                break;
        }
    }
    private void requestData(String url,String channelRCId){
        OkHttpUtils.get()
                .url(url+channelRCId)
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMaker.showToastShort(R.string.generic_error);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson=new Gson();
                        GroupsDetail groupsDetail = gson.fromJson(response, GroupsDetail.class);
                        if (groupsDetail.getStatus()==200
                                && groupsDetail.getData() != null) {
                            mPhoneTvHuatiName.setText(groupsDetail.getData().getChannelName());
                            mPhoneTvHuatiId.setText(groupsDetail.getData().getChannelId());
                            mPhoneTvHuatiMember.setText(""+groupsDetail.getData().getTotalPer()+"");
                        }
                    }
                });
    }

    private void deleQUN(String url,String userId,String channelId){
        OkHttpUtils.post()
                .url(url)
                .addParams("userId",userId)
                .addParams("channelId",channelId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMaker.showToastShort(R.string.delete_groupchat_fail);
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        CommonResult result = gson.fromJson(response, CommonResult.class);
                        if (result != null && result.status == 200){
                            ToastMaker.showToastShort(R.string.delete_groupchat_success);

                            Intent intent = new Intent();
                            intent.putExtra("delete", true);
                            setResult(RESULT_OK, intent);
                            finish();
                        }else{
                            ToastMaker.showToastShort(R.string.delete_groupchat_fail);
                        }
                    }
                });
    }
}
